package com.example.mqtt_client.singleton;

import com.example.mqtt_client.config.DBAuthenticatorConfig;

import io.moquette.broker.ISslContextCreator;
import io.moquette.broker.Server;
import io.moquette.broker.config.ClasspathResourceLoader;
import io.moquette.broker.config.IConfig;
import io.moquette.broker.config.IResourceLoader;
import io.moquette.broker.config.ResourceLoaderConfig;
import io.moquette.broker.security.DBAuthenticator;
import io.moquette.interception.AbstractInterceptHandler;
import io.moquette.interception.InterceptHandler;
import io.moquette.interception.messages.InterceptAcknowledgedMessage;
import io.moquette.interception.messages.InterceptConnectMessage;
import io.moquette.interception.messages.InterceptDisconnectMessage;
import io.moquette.interception.messages.InterceptPublishMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;

public enum Singleton {

    INSTANCE;
    private Server mqttBroker = new Server();

    @Resource
    private DBAuthenticatorConfig dbAuthenticatorConfig;

    static class PublisherListener extends AbstractInterceptHandler {

        @Override
        public String getID() {
            return "EmbeddedLauncherPublishListener";
        }

        @Override
        public void onPublish(InterceptPublishMessage msg) {
            System.out.println("消息已发送");
        }

        @Override
        public void onConnect(InterceptConnectMessage msg) {
            String username = msg.getUsername();
            System.out.println(username+"已上线");
        }

        @Override
        public void onDisconnect(InterceptDisconnectMessage msg) {
            String username = msg.getUsername();
            System.out.println(username+"已离线");
        }

    }

    private Singleton() {
        IResourceLoader classpathLoader = new ClasspathResourceLoader();
        final IConfig classPathConfig = new ResourceLoaderConfig(classpathLoader);

//        DBConnection db = new DBConnection();
//        DBAuthenticator auth = new DBAuthenticator(db.getDriver(), db.getUrl(), db.getSqlQuery(),db.getDigestMethod());
        List<? extends InterceptHandler> userHandlers = Collections.singletonList(new PublisherListener());

        try {
            mqttBroker.startServer(classPathConfig, userHandlers,null,dbAuthenticatorConfig,null);
        } catch (Exception e) {
            System.err.println(e);
        }
        System.out.println("Broker started press [CTRL+C] to stop");
        //Bind  a shutdown hook
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("Stopping broker");
            mqttBroker.stopServer();
            System.out.println("Broker stopped");
        }));
    }

    public Server getMqttBroker() {
        return mqttBroker;
    }
}
